package com.wnxy.product.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 消息队列的配置类
 */
@Configuration
public class RabbitConfig {

    /**
     * 创建一个邮件队列
     */
    @Bean
    public Queue emailQueue() {
        return new Queue("msg.queue");
    }

    @Bean
    public Queue ObjQueue() {
        return new Queue("obj.queue");
    }


    @Bean
    public Queue simpleQueue() {
        return new Queue("simple.queue");
    }

    @Bean
    public Queue workQueue() {
        return new Queue("work.queue");
    }

    @Bean
    public Queue fanoutQueue1() {
        return new Queue("fanout.queue1");
    }

    @Bean
    public Queue fanoutQueue2() {
        return new Queue("fanout.queue2");
    }

    /**
     * 交换机
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanout.exchange");
    }

    @Bean
    public Binding fanoutBinding1(Queue fanoutQueue1, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }

    @Bean
    public Binding fanoutBinding2(Queue fanoutQueue2, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }

    @Bean
    public Queue directQueue1() {
        return new Queue("direct.queue1");
    }

    @Bean
    public Queue directQueue2() {
        return new Queue("direct.queue2");
    }

    /**
     * 交换机
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("direct.exchange");
    }

    @Bean
    public Binding directBinding1(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with("shuai");
    }

    @Bean
    public Binding directBinding2(Queue directQueue2, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue2).to(directExchange).with("handsome");
    }

    @Bean
    public Queue topicQueue1() {
        return new Queue("topic.queue1");
    }

    @Bean
    public Queue topicQueue2() {
        return new Queue("topic.queue2");
    }

    /**
     * 交换机
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topic.exchange");
    }

    @Bean
    public Binding topicBinding1(Queue topicQueue1, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue1).to(topicExchange).with("shuaige.*.news");
    }

    @Bean
    public Binding topicBinding2(Queue topicQueue2, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with("#.news");
    }

    /**
     * 普通队列： normal.queue
     * 普通交换机： normal.exchange
     * 队列绑定到交换机： city.gz
     * <p>
     * 死信队列： dlx.queue
     * 死信交换机： dlx.exchange
     * 死信队列绑定到死信交换机： dlx.routing.key
     */
    @Bean
    public Queue normalQueue() {
        return QueueBuilder.durable("normal.queue")//普通队列名字
                .deadLetterExchange("dlx.exchange")//死信交换机
                .deadLetterRoutingKey("dlx.routing.key")//死信路由键
                .ttl(30000)//30s以后消息进入死信队列
                .build();
    }

    @Bean
    public DirectExchange normalExchange() {
        return new DirectExchange("normal.exchange");
    }

    @Bean
    public Binding confirmToDirectExchange(Queue normalQueue, DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("city.gz");
    }

    @Bean
    public Queue dlxQueue() {
        return new Queue("dlx.queue");
    }

    @Bean
    public DirectExchange dxlExchange() {
        return new DirectExchange("dlx.exchange");
    }

    @Bean
    public Binding dlxToDirectExchange(Queue dlxQueue, DirectExchange dxlExchange) {
        return BindingBuilder.bind(dlxQueue).to(dxlExchange).with("dlx.routing.key");
    }

}